require 'rubygems'
require 'rstack/version'
require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'spec/rake/spectask'

module RStack
  class Configuration
    
    attr_accessor :name, :summary, :author, :email, :url, :version,
                  :platform, :spec_globs, :package_globs, :require_path,
                  :has_rdoc, :out_path, :executables, :extra_dependencies
    
    def initialize      
      @name           = '[ENTER A NAME]'
      @summary        = '[ENTER A SUMMARY]'
      @author         = '[ENTER A AUTHOR]'
      @email          = '[ENTER YOUR EMAIL]'
      @url            = '[ENTER A PROJECT URL]'
      @version        = '0.1.0'
      @platform       = Gem::Platform::RUBY  
      @spec_globs     = FileList['spec/**/*_spec.rb']
      @package_globs  = FileList['[a-zA-Z]*', 'bin/**/*', 'lib/**/*', 'spec/**/*']
      @require_path   = 'lib'
      @has_rdoc       = false
      @out_path       = ENV["CC_BUILD_ARTIFACTS"] || 'out'
      @executables    = []
      @extra_dependencies = []
                        
      yield self if block_given?
      
      @modulized_name = @name.pathize.classify
      define_tasks
      
      out_dir = FileUtils.getwd + '/out'
      FileUtils.mkdir(out_dir) unless File.exist?(out_dir)
    end
    
    def define_tasks
      task :default => :spec
      task :cruise => [:repackage, :rdoc]
      task :repackage => "spec:full"

      Spec::Rake::SpecTask.new do |t|
        t.spec_files = @spec_glob
      end
 
      desc "Run specs through RCov and generate HTML reports"
      Spec::Rake::SpecTask.new('spec:full') do |t|
        t.rcov_dir      = @out_path + '/coverage'
        t.spec_files    = @spec_globs
        t.spec_opts     = ["--format", "html:#{@out_path}/spec_report.html", "--diff"]
        t.fail_on_error = false
        t.rcov          = true
        t.rcov_opts << '--no-rcovrt' # required or rcov will fail
      end
      
      Gem::manage_gems

      spec = Gem::Specification.new do |s|
        s.name      = @name
        s.version   = @version
        s.platform  = @platform
        s.summary   = @summary
        s.author    = @author
        s.email     = @email
        s.homepage  = @url

        s.add_dependency('rstack',    ">= #{RStack::Version::STRING}") unless @name == 'rstack'
        s.add_dependency('rake',      '>= 0.7.2')
        s.add_dependency('rcov',      '>= 0.8.0')
        s.add_dependency('diff-lcs',  '>= 1.1.2')        
        s.add_dependency('rspec',     '>= 1.0.5')

        @extra_dependencies.each do |dependency|
          s.add_dependency(dependency)
        end
        
        s.files = @package_globs.to_a.delete_if {|file| file.include?('.svn') || file.include?('rdoc') }

        s.executables       = @executables
        s.require_path      = @require_path
        s.has_rdoc          = @has_rdoc
        s.extra_rdoc_files  = ["README.txt"]
      end
      Rake::GemPackageTask.new(spec) { |p| p.gem_spec = spec }

      Rake::RDocTask.new do |rdoc|
        rdoc.rdoc_dir = @out_path + '/doc'
        rdoc.title = "#{@modulized_name} -- #{@version} -- #{@summary}"
        rdoc.options << '--line-numbers' << '--inline-source'
        rdoc.rdoc_files.push *spec.files.grep(/^(lib|bin)|txt$/)
      end      
    end
  end
end